package com.ruoyi.users.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.users.domain.StudyPlansContents;
import com.ruoyi.users.service.StudyPlansContentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.users.mapper.UStudyPlansMapper;
import com.ruoyi.users.domain.UStudyPlans;
import com.ruoyi.users.service.IUStudyPlansService;

/**
 * 学习计划Service业务层处理
 *
 * @author Amy
 * @date 2024-12-01
 */
@Service
public class UStudyPlansServiceImpl implements IUStudyPlansService
{
    @Autowired
    private UStudyPlansMapper uStudyPlansMapper;
    @Autowired
    private StudyPlansContentsService studyPlansContentsService;

    /**
     * 查询学习计划
     *
     * @param id 学习计划主键
     * @return 学习计划
     */
    @Override
    public UStudyPlans selectUStudyPlansById(Long id)
    {
        UStudyPlans uStudyPlans = uStudyPlansMapper.selectUStudyPlansById(id);
        StudyPlansContents contentById = studyPlansContentsService.findContentById(id.toString());
        uStudyPlans.setContent(contentById.getContent());
        return uStudyPlans;
    }

    /**
     * 查询学习计划列表
     *
     * @param uStudyPlans 学习计划
     * @return 学习计划
     */
    @Override
    public List<UStudyPlans> selectUStudyPlansList(UStudyPlans uStudyPlans)
    {
        List<UStudyPlans> uStudyPlanss = uStudyPlansMapper.selectUStudyPlansList(uStudyPlans);
        uStudyPlanss= uStudyPlanss.stream().map(item->{
            StudyPlansContents contentById = studyPlansContentsService.findContentById(item.getId().toString());
            item.setContent(contentById.getContent());
            return item;
        }).collect(Collectors.toList());

        return uStudyPlanss;
    }

    /**
     * 新增学习计划
     *
     * @param uStudyPlans 学习计划
     * @return 结果
     */
    @Override
    public int insertUStudyPlans(UStudyPlans uStudyPlans)
    {
        uStudyPlans.setCreateTime(DateUtils.getNowDate());
        int i = uStudyPlansMapper.insertUStudyPlans(uStudyPlans);
        StudyPlansContents studyPlansContents = new StudyPlansContents();
        studyPlansContents.setId(uStudyPlans.getId().toString());
        studyPlansContents.setContent(uStudyPlans.getContent());
        studyPlansContentsService.saveContent(studyPlansContents);
        return i;

    }

    /**
     * 修改学习计划
     *
     * @param uStudyPlans 学习计划
     * @return 结果
     */
    @Override
    public int updateUStudyPlans(UStudyPlans uStudyPlans)
    {
        uStudyPlans.setUpdateTime(DateUtils.getNowDate());
        int i = uStudyPlansMapper.updateUStudyPlans(uStudyPlans);
        StudyPlansContents studyPlansContents = new StudyPlansContents();
        studyPlansContents.setId(uStudyPlans.getId().toString());
        studyPlansContents.setContent(uStudyPlans.getContent());
        studyPlansContentsService.updateContent(studyPlansContents);
        return i;
    }

    /**
     * 批量删除学习计划
     *
     * @param ids 需要删除的学习计划主键
     * @return 结果
     */
    @Override
    public int deleteUStudyPlansByIds(Long[] ids)
    {
        int i = uStudyPlansMapper.deleteUStudyPlansByIds(ids);
        for (Long id : ids) {
            studyPlansContentsService.deleteContentById(id.toString());
        }
        return i;

    }

    /**
     * 删除学习计划信息
     *
     * @param id 学习计划主键
     * @return 结果
     */
    @Override
    public int deleteUStudyPlansById(Long id)
    {

        int i = uStudyPlansMapper.deleteUStudyPlansById(id);
        studyPlansContentsService.deleteContentById(id.toString());
        return i;
    }
}


